/**
 * 
 */
package org.vsg.security.authorization;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import org.jsecurity.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.vsg.security.model.domain.UrlResource;
import org.vsg.security.model.persistence.UrlResourceDao;
import org.vsg.security.vo.Auth;
import org.vsg.security.vo.AuthUrl;
import org.vsg.security.vo.Controller;

/**
 * @author Bill Vison
 *
 */
public class BasicURLCompAuthorization implements URLCompAuthorization {

	private static final Logger logger = LoggerFactory.getLogger(BasicURLCompAuthorization.class);
	
	
	private UrlResourceDao urlResourceDao;	

	public UrlResourceDao getUrlResourceDao() {
		return urlResourceDao;
	}

	public void setUrlResourceDao(UrlResourceDao urlResourceDao) {
		this.urlResourceDao = urlResourceDao;
	}

	/* (non-Javadoc)
	 * @see org.vsg.security.authorization.URLCompAuthorization#authorization(org.jsecurity.subject.Subject, java.lang.String, java.lang.String)
	 */
	@Override
	public Auth authorization(Subject currentUser, String module, String urlCode) {
		// TODO Auto-generated method stub
		Auth auth =  new Auth();
		
		Collection<UrlResource> allResources = urlResourceDao.findUrlResourceByCode(urlCode, module);
		
		List<AuthUrl> all = new Vector<AuthUrl>();
		UrlResource urlResource = null;
		for(Iterator<UrlResource> resIter = allResources.iterator() ; resIter.hasNext(); ) {
			urlResource = resIter.next();
			
			AuthUrl authUrl = new AuthUrl();
			authUrl.setUrl( urlResource.getUrl() );
			Controller controller = new Controller();
			controller.setValue("Vsg.AuthorisingRule.URLRule.Visitatorial");
			authUrl.setController( controller );
			all.add( authUrl );
			auth.setAuthUrls(all);
		}
		
		return auth;
	}

}
